package cn.iocoder.yudao.module.bpm.controller.admin.tohandle.flow;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.bpm.controller.admin.tohandle.flow.vo.*;
import cn.iocoder.yudao.module.bpm.convert.tohandle.flow.BpmHandleFlowConvert;
import cn.iocoder.yudao.module.bpm.dal.dataobject.tohandle.BpmHandleFlowDO;
import cn.iocoder.yudao.module.bpm.service.tohandle.flow.BpmHandleFlowService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 审批流待办中心——流程flow表")
@RestController
@RequestMapping("/bpm/handle-flow")
@Validated
public class BpmHandleFlowController {

    @Resource
    private BpmHandleFlowService handleFlowService;

    @PostMapping("/create")
    @Operation(summary = "创建审批流待办中心——流程flow表")
    @PreAuthorize("@ss.hasPermission('bpm:handle-flow:create')")
    public CommonResult<Long> createHandleFlow(@Valid @RequestBody BpmHandleFlowCreateReqVO createReqVO) {
        return success(handleFlowService.createHandleFlow(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新审批流待办中心——流程flow表")
    @PreAuthorize("@ss.hasPermission('bpm:handle-flow:update')")
    public CommonResult<Boolean> updateHandleFlow(@Valid @RequestBody BpmHandleFlowUpdateReqVO updateReqVO) {
        handleFlowService.updateHandleFlow(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除审批流待办中心——流程flow表")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('bpm:handle-flow:delete')")
    public CommonResult<Boolean> deleteHandleFlow(@RequestParam("id") Long id) {
        handleFlowService.deleteHandleFlow(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得审批流待办中心——流程flow表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('bpm:handle-flow:query')")
    public CommonResult<BpmHandleFlowRespVO> getHandleFlow(@RequestParam("id") Long id) {
        BpmHandleFlowDO handleFlow = handleFlowService.getHandleFlow(id);
        return success(BpmHandleFlowConvert.INSTANCE.convert(handleFlow));
    }

    @GetMapping("/list")
    @Operation(summary = "获得审批流待办中心——流程flow表列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('bpm:handle-flow:query')")
    public CommonResult<List<BpmHandleFlowRespVO>> getHandleFlowList(@RequestParam("ids") Collection<Long> ids) {
        List<BpmHandleFlowDO> list = handleFlowService.getHandleFlowList(ids);
        return success(BpmHandleFlowConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得审批流待办中心——流程flow表分页")
    @PreAuthorize("@ss.hasPermission('bpm:handle-flow:query')")
    public CommonResult<PageResult<BpmHandleFlowRespVO>> getHandleFlowPage(@Valid BpmHandleFlowPageReqVO pageVO) {
        PageResult<BpmHandleFlowDO> pageResult = handleFlowService.getHandleFlowPage(pageVO);
        return success(BpmHandleFlowConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出审批流待办中心——流程flow表 Excel")
    @PreAuthorize("@ss.hasPermission('bpm:handle-flow:export')")
    @OperateLog(type = EXPORT)
    public void exportHandleFlowExcel(@Valid BpmHandleFlowExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BpmHandleFlowDO> list = handleFlowService.getHandleFlowList(exportReqVO);
        // 导出 Excel
        List<BpmHandleFlowExcelVO> datas = BpmHandleFlowConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "审批流待办中心——流程flow表.xls", "数据", BpmHandleFlowExcelVO.class, datas);
    }

}
